import 'package:flutter/material.dart';
import 'package:flutter_app_study/cost/Cols.dart';
import 'package:flutter_app_study/utils/ImageUtil.dart';
import 'package:flutter_app_study/utils/ScreenAdapter.dart';
import 'package:flutter_spinkit/flutter_spinkit.dart';

//四种视图状态
enum LoadState { State_Success, State_Error, State_Loading, State_Empty }

//根据不同状态来展示不同的视图
class LoadStateLayout extends StatefulWidget {
  final LoadState state; //页面状态
  final Widget successWidget; //成功视图
  final VoidCallback errorRetry; //错误事件处理
  final VoidCallback emptyRetry; //空数据事件处理

  LoadStateLayout(
      {Key key,
      this.state = LoadState.State_Loading, //默认为加载状态
      this.successWidget,
      this.errorRetry,
      this.emptyRetry})
      : super(key: key);

  @override
  _LoadStateLayoutState createState() => _LoadStateLayoutState();
}

class _LoadStateLayoutState extends State<LoadStateLayout> {
  @override
  Widget build(BuildContext context) {
    return Container(
      width: double.infinity,
      height: double.infinity,
      child: _buildWidget,
    );
  }

  Widget get _buildWidget {
    switch (widget.state) {
      case LoadState.State_Success:
        return widget.successWidget;
        break;
      case LoadState.State_Error:
        return ErrorView(widget.errorRetry);
        break;
      case LoadState.State_Loading:
        return LoadingView();
        break;
      case LoadState.State_Empty:
        return NoDataView(widget.emptyRetry);
        break;
      default:
        return null;
    }
  }
}

class LoadingView extends StatefulWidget {
  @override
  _LoadingViewState createState() => _LoadingViewState();
}

class _LoadingViewState extends State<LoadingView> {
  @override
  Widget build(BuildContext context) {
    return Container(
      width: double.infinity,
      height: double.infinity,
      alignment: Alignment.center,
      color: Colors.white,
      child: Container(
        height: 200,
        padding: EdgeInsets.all(10),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.spaceAround,
          children: <Widget>[
            Container(
              width: 150,
              height: 150,
              child: SpinKitCircle(
                color: Colors.black,
                size: 50.0,
              ),
            ),
            Text(
              '拼命加载中...',
              style: TextStyle(
                  color: Cols.text, fontSize: ScreenAdapter().setSp(24)),
            )
          ],
        ),
      ),
    );
  }
}

class ErrorView extends StatefulWidget {
  final VoidCallback errorRetry;

  ErrorView(this.errorRetry);

  @override
  _ErrorViewState createState() => _ErrorViewState();
}

class _ErrorViewState extends State<ErrorView> {
  @override
  Widget build(BuildContext context) {
    return Container(
        width: double.infinity,
        height: double.infinity,
        color: Colors.white,
        child: InkWell(
          onTap: widget.errorRetry,
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              Container(
                width: 405,
                height: 317,
                child: Image.asset(ImageUtil.getImageAsset("header_my.png")),
              ),
              Text(
                "加载失败，请轻触重试!",
                style: TextStyle(
                    color: Cols.text, fontSize: ScreenAdapter().setSp(24)),
              ),
            ],
          ),
        ));
  }
}

class NoDataView extends StatefulWidget {
  final VoidCallback emptyRetry; //无数据事件处理

  NoDataView(this.emptyRetry);

  @override
  _NoDataViewState createState() => _NoDataViewState();
}

class _NoDataViewState extends State<NoDataView> {
  @override
  Widget build(BuildContext context) {
    return Container(
        color: Colors.white,
        width: ScreenAdapter().setWidth(750),
        height: double.infinity,
        child: InkWell(
          onTap: widget.emptyRetry,
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              Container(
                width: ScreenAdapter().setWidth(405),
                height: ScreenAdapter().setHeight(281),
                child: Image.asset(ImageUtil.getImageAsset("2.png")),
              ),
              Text(
                '暂无相关数据,轻触重试~',
                style: TextStyle(
                    color: Cols.text, fontSize: ScreenAdapter().setSp(24)),
              )
            ],
          ),
        ));
  }
}
